<?php
namespace Admin\Controller;
use Think\Controller;
class AuthController extends AdminController {
    public function index(){
		
		$this->display();
        //$this->show('<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} body{ background: #fff; font-family: "微软雅黑"; color: #333;font-size:24px} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.8em; font-size: 36px } a,a:hover,{color:blue;}</style><div style="padding: 24px 48px;"> <h1>:)</h1><p>欢迎使用 <b>ThinkPHP</b>！</p><br/>版本 V{$Think.version}</div><thinkad id="ad_55e75dfae343f5a1"></thinkad><script type="text/javascript" src="http://tajs.qq.com/stats?sId=9347272" charset="UTF-8"></script>','utf-8');
    }

    /**
     * 用户组授权用户列表
     * @author 丁小雨 <614422099@qq.com>
     */
    public function group_user($gid){
        if(empty($gid)){
            $this->error('参数错误');
        }
        
        $prefix   = C('DB_PREFIX');
        // $l_table  = $prefix.'member';
        // $r_table  = $prefix."auth_group_access";
        // $model    = M()->table( $l_table.' m' )->join ( $r_table.' a ON m.uid=a.uid' )->select();
        // $_REQUEST = array();
        // dump($model);
        //$list = $this->lists($model,array('a.group_id'=>$group_id,'m.status'=>array('egt',0)),'m.uid asc','m.uid,m.nickname,m.last_login_time,m.last_login_ip,m.status');
        //int_to_string($list);

        $list = D('AuthGroupAccess')->where(array('group_id' => $gid))->select();
        $member = D('Member');

        foreach ($list as $key => $value) {
        	$list[$key] += $member->where('uid = '.$value['uid'])->find();
        }
        //dump($list);
        $this->assign( '_list',     $list );
        $this->assign( 'gid',     $gid );
		
        $this->meta_title = '成员授权';
        $this->display();
    }

    public function group_user_del($gid , $uid){
    	$list = D('AuthGroupAccess')->where(array('group_id' => $gid , 'uid'=>$uid))->find();
    	if (empty($list)) {
    		$this->error("用户组内未找到对应的用户！");
    	}
    	$ret = D('AuthGroupAccess')->where(array('group_id' => $gid , 'uid'=>$uid))->delete();

    	if($ret > 0){
    		$this->success('用户删除成功！');
    	}else{
    		$this->error("用户删除失败，请联系管理员！");
    	}
    }

    //为用户组添加用户
    public function gruop_user_add($gid = 0 , $username = '',$uid = 0){
    	    if(empty($gid) or $gid == 0){
	    		$this->error("用户组ID错误");
	    	}
    		$AuthGroup = D('AuthGroup');
	    	$ret = $AuthGroup->where(array('id'=>$gid))->find();
	    	if (empty($ret)) {
	    		$this->error("未找到用户组ID");
	    	}



	    	$member = D('Member');

	    	$arr = array();
	    	if($username != '') $arr['username'] = $username;
	    	if($uid != 0) $arr['uid'] = $uid;

	    		//查询用户资料
	    		$user = $member->where($arr)->find();
	    		if (empty($user)) {
	    			$this->error("未找到用户‘".$username);
	    		}

	    		//用户组内已存在成员
		    	$list = D('AuthGroupAccess')->where(array('group_id' => $gid , 'uid'=>$user['uid']))->find();
		    	if (!empty($list)) {
		    		$this->error("用户组已存在用户！");
		    	}

	    		$ret = D('AuthGroupAccess')->add(array('uid' => $user['uid'],'group_id' => $gid));
	    		if($ret > 0){
	    			$this->success('用户添加成功！');
	    		}else{
	    			$this->error("用户添加失败！");
	    		}
	    		exit();

    }

    public function group(){
    	$list = D('AuthGroup')->order('module')->limit(10)->select();
    	$this->assign('_list', $list);
    	$this->meta_title = '用户组列表';
    	$this->display();
    }

    public function group_edit($id = 0){
    		if(empty($id) or $id == 0){
	    		$this->error("ID错误");
	    	}
	    	$AuthGroup = D('AuthGroup');
	    	$ret = $AuthGroup->where(array('id'=>$id))->find();
	    	if (empty($ret)) {
	    		$this->error("未找到ID");
	    	}

    	if(IS_POST){
    		$rules = arr2str(I("rules"));

            $data = array('module' => I("module"), 'type' =>1 , 'rules' => $rules, 'status' => 1 ,
				'title' => I("title"),'description'=>I("description"));

            if (!$AuthGroup->create($data)){
                 // 对data数据进行验证
                $this->error($AuthGroup->getError());
            }else{     
            	// 验证通过 可以进行其他数据操作
                if(false === $AuthGroup->where(array('id'=>$id))->save($arr)){
                    $this->error('用户组修改失败！');
                } else {
                    $this->success('用户组修改成功！',U('group'));
                }
            }
    	}else{


	    	$this->assign('ret', $ret);

	 		$ret_arr = str2arr($ret['rules']);

	    	$list = D('AuthRule')->order('module')->select();
	 		foreach ($list as $key => $value) {
	 			if(in_array($value[id], $ret_arr)){
	 				$list[$key]['selected'] = 'selected';
	 			}else{
	 				$list[$key]['selected'] = '';
	 			}
	 			//dump($value);
	 		}
	 		//dump($ret_arr);
	 		//dump($list);
	 		    //权限列表
	    		$this->assign('_list', $list);
	    		$this->assign('id', $id);
	    	$this->meta_title = '编辑用户组';
	    	$this->display();
	    }
    	
    }

    public function group_del($id = 0){
    	if(empty($id) or $id == 0){
    		$this->error("ID错误");
    	}

    	$AuthGroup = D('AuthGroup');
    	$list = $AuthGroup->where(array('id'=>$id))->select();
    	if (empty($list)) {
    		$this->error("未找到ID");
    	}
    	$ret = $AuthGroup->where(array('id'=>$id))->delete(); 
    	if($ret > 0){
    		$this->success('用户组删除成功！');
    	}
    }

    public function group_add(){
    	if(IS_POST){

    		debug_dump(I());
    		$rules = arr2str(I("rules"));

            $data = array('module' => I("module"), 'type' =>1 , 'rules' => $rules, 'status' => 1 ,
				'title' => I("title"),'description'=>I("description"));
			$AuthGroup = D('AuthGroup');

            if (!$AuthGroup->create($data)){
                 // 对data数据进行验证
                $this->error($AuthGroup->getError());
            }else{     
            	// 验证通过 可以进行其他数据操作
                if(!$AuthGroup->add($arr)){
                    $this->error('用户组添加失败！');
                } else {
                    $this->success('用户组添加成功！',U('group'));
                }
            }
        } else {
        	$list = D('AuthRule')->order('module')->select();
    		$this->assign('_list', $list);
            $this->meta_title = '新增用户组';
            $this->display();
        }
    }

    public function node(){
    	C('SHOW_PAGE_TRACE',false);

    	$list = D('AuthRule')->order('module')->limit(10)->select();
    	$this->assign('_list', $list);
    	$this->meta_title = '节点列表';
    	$this->display();
    }

    public function node_add(){
    	if(IS_POST){

        	$type = I("type");
        	if($type != 1 and $type != 2){
        		$this->error('类型选择错误！');
        	}

            $data = array('module' => I("module"), 'type' =>$type, 'name' => I("name") , 'status' => 1 ,
				'title' => I("title"),'condition'=>I("condition"),
			);
			$AuthRule = D('AuthRule');

            if (!$AuthRule->create($data)){
                 // 对data数据进行验证
                $this->error($AuthRule->getError());
            }else{     
            // 验证通过 可以进行其他数据操作
                if(!$AuthRule->add($arr)){
                    $this->error('节点添加失败！');
                } else {
                    $this->success('节点添加成功！',U('node'));
                }
            }
        } else {
            $this->meta_title = '新增节点';
            $this->display();
        }
    }

    public function node_del($id = 0){
    	if(empty($id) or $id == 0){
    		$this->error("ID错误");
    	}

    	$AuthRule = D('AuthRule');
    	$list = $AuthRule->where(array('id'=>$id))->select();
    	if (empty($list)) {
    		$this->error("未找到ID");
    	}
    	$ret = $AuthRule->where(array('id'=>$id))->delete(); 
    	if($ret > 0){
    		$this->success('节点删除成功！');
    	}
    }

    public function node_edit($id = 0){
    		if(empty($id) or $id == 0){
	    		$this->error("ID错误");
	    	}
	    	$AuthRule = D('AuthRule');
	    	$ret = $AuthRule->where(array('id'=>$id))->find();
	    	if (empty($ret)) {
	    		$this->error("未找到ID");
	    	}

    	if(IS_POST){

            $arr = array('module' => I("module"), 'type' =>$type, 'name' => I("name") , 'status' => 1 ,
				'title' => I("title"),'condition'=>I("condition"));

            if (!$AuthRule->create($arr)){
                 // 对data数据进行验证
                $this->error($AuthRule->getError());
            }else{     
            	// 验证通过 可以进行其他数据操作
                if(false === $AuthRule->where(array('id'=>$id))->save($arr)){
                    $this->error('用户组修改失败！');
                } else {
                    $this->success('用户组修改成功！',U('node'));
                }
            }
    	}else{


	    	$this->assign('ret', $ret);
	    	$this->assign('id', $id);
	    	//dump($ret);

	    	$this->meta_title = '编辑用户组';
	    	$this->display();
	    }
    	
    }
}